﻿using System.Collections.Generic;
using KMF.Common;
using KMF.DAL;

namespace KMF.WebUI
{
    public class ConfigInfo
    {
        public static Dictionary<string, string> WebInfoList;

        public static void LoadData()
        {
            WebInfoList = new Dictionary<string, string>();
            var configList = ConfigDb.GetAll() ?? new List<ConfigDto>();
            foreach (var configDto in configList)
            {
                // tranh trung key
                if (!WebInfoList.ContainsKey(configDto.Key))
                {
                    WebInfoList.Add(configDto.Key, configDto.Value);
                }
            }
        }

        public static string GetByKey(string key)
        {
            var val = WebInfoList.ContainsKey(key) ? WebInfoList[key] : "";
            return !string.IsNullOrEmpty(val) ? val : "";
        }

        public static string GetByKey(string key, string type, string value, string description)
        {
            var val = WebInfoList.ContainsKey(key) ? WebInfoList[key] : "";
            if (!string.IsNullOrEmpty(val)) return val;
            AddValue(key, type, value, description);
            return value;
        }

        public static void AddValue(string key, string type, string value, string description)
        {
            ConfigDb.Add(new ConfigDto { Key = key, Value = value, Description = description, Type = type });
            WebInfoList.Add(key, value);
        }

    }
}
